﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Threading;
using System.Globalization;
using System.Resources;
using System.Data;

namespace Phoenix
{
    public partial class Main : System.Web.UI.MasterPage
    {
        ResourceManager rm;
        CultureInfo ci;
        //ty gia
        string url = "http://vietcombank.com.vn/ExchangeRates/ExrateXML.aspx";
        //ty gia
        Phoenix.AppCode.Entities.Article left_cag = new Phoenix.AppCode.Entities.Article();
        Phoenix.AppCode.Entities.Counter online = new Phoenix.AppCode.Entities.Counter();
        Phoenix.AppCode.Entities.CorpInfo corpinfo = new Phoenix.AppCode.Entities.CorpInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie fenix_lang = Request.Cookies["Culture"];
            if (fenix_lang == null)
            {
                fenix_lang = new HttpCookie("Culture", "en-US");
                fenix_lang.Expires = DateTime.Now.AddHours(24);
                Response.Cookies.Add(fenix_lang);
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
                ci = Thread.CurrentThread.CurrentCulture;
                Load_Header(ci);
                Load_Footer(ci);
                LoadDB_US();
                LoadContactUS();
            }
            else
            {
                if (fenix_lang.Value == "vi-VN")
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
                    rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
                    ci = Thread.CurrentThread.CurrentCulture;
                    Load_Header(ci);
                    Load_Footer(ci);
                    LoadDB_VN();
                    LoadContactVN();
                }
                else
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                    rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
                    ci = Thread.CurrentThread.CurrentCulture;
                    Load_Header(ci);
                    Load_Footer(ci);
                    LoadDB_US();
                    LoadContactUS();
                }
            }
            getCounter();
            //ty gia
            tygia();
            //ty gia

        }
        //ty gia
        private void tygia()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(url);
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = new DataTable();
                dt = ds.Tables["Exrate"];
                DataRow dr0 = dt.Rows[0];
                string aud = "";
                aud += "<td>" + dr0["CurrencyCode"].ToString() + "</td><td>" + dr0["Buy"].ToString() + "</td><td>" + dr0["Sell"].ToString() + "</td>";
                tg_aud.Text = aud;

                DataRow dr4 = dt.Rows[4];
                string eur = "";
                eur += "<td>" + dr4["CurrencyCode"].ToString() + "</td><td>" + dr4["Buy"].ToString() + "</td><td>" + dr4["Sell"].ToString() + "</td>";
                tg_eur.Text = eur;

                DataRow dr5 = dt.Rows[5];
                string gbp = "";
                gbp += "<td>" + dr5["CurrencyCode"].ToString() + "</td><td>" + dr5["Buy"].ToString() + "</td><td>" + dr5["Sell"].ToString() + "</td>";
                tg_gbp.Text = gbp;

                DataRow dr8 = dt.Rows[8];
                string jpy = "";
                jpy += "<td>" + dr8["CurrencyCode"].ToString() + "</td><td>" + dr8["Buy"].ToString() + "</td><td>" + dr8["Sell"].ToString() + "</td>";
                tg_jpy.Text = jpy;

                DataRow dr17 = dt.Rows[17];
                string usd = "";
                usd += "<td>" + dr17["CurrencyCode"].ToString() + "</td><td>" + dr17["Buy"].ToString() + "</td><td>" + dr17["Sell"].ToString() + "</td>";
                tg_usd.Text = usd;
            }
        }
        //ty gia
        public void Load_Header(CultureInfo ci)
        {
            //top menu
            string home = rm.GetString("lb_home", ci);
            string about = rm.GetString("lb_aboutus", ci);
            string service = rm.GetString("lb_services", ci);
            string partner = rm.GetString("lb_partners", ci);
            string shipping = rm.GetString("lb_shippingschedule", ci);
            string tracking = rm.GetString("lb_tracking", ci);
            string recruiment = rm.GetString("lb_recruitment", ci);
            string contact = rm.GetString("lb_contactus", ci);
            lb_home.Text = home;
            lb_aboutus.Text = about;
            lb_services.Text = service;
            lb_partners.Text = partner;
            lb_shippingschedule.Text = shipping;
            lb_tracking.Text = tracking;
            lb_recruitment.Text = recruiment;
            lb_contactus.Text = contact;
            //left cag
            lb_cag.Text = service;
            string currency = rm.GetString("lb_tg", ci);
            lb_tg.Text = currency;
        }
        public void Load_Footer(CultureInfo ci)
        {
            //bottom menu
            string home = rm.GetString("lb_home", ci);
            string about = rm.GetString("lb_aboutus", ci);
            string service = rm.GetString("lb_services", ci);
            string partner = rm.GetString("lb_partners", ci);
            string shipping = rm.GetString("lb_shippingschedule", ci);
            string tracking = rm.GetString("lb_tracking", ci);
            string recruiment = rm.GetString("lb_recruitment", ci);
            string contact = rm.GetString("lb_contactus", ci);
            lb_fhome.Text = home;
            lb_faboutus.Text = about;
            lb_fservices.Text = service;
            lb_fpartners.Text = partner;
            lb_fshippingschedule.Text = shipping;
            lb_ftracking.Text = tracking;
            lb_frecruitment.Text = recruiment;
            lb_fcontactus.Text = contact;
            //count visit
            string online1 = rm.GetString("online1", ci);
            string online2 = rm.GetString("online2", ci);
            lb_online1.Text = online1;
            lb_online2.Text = online2;
            //ty gia
            string buy = rm.GetString("lb_buy", ci);
            string sell = rm.GetString("lb_sell", ci);
            lb_buy.Text = buy;
            lb_sell.Text = sell;
        }

        protected void lang_vi_Click(object sender, EventArgs e)
        {
            HttpCookie fenix_lang = new HttpCookie("Culture", "vi-VN");
            fenix_lang.Expires = DateTime.Now.AddHours(24);
            Response.Cookies.Add(fenix_lang);
            Response.Redirect(Request.Url.AbsoluteUri);
            string vi_lang = "vi-VN";
            Thread.CurrentThread.CurrentCulture = new CultureInfo(vi_lang.ToString());
            Load_Header(Thread.CurrentThread.CurrentCulture);
            Load_Footer(Thread.CurrentThread.CurrentCulture);
        }

        protected void lang_en_Click(object sender, EventArgs e)
        {
            HttpCookie fenix_lang = new HttpCookie("Culture", "en-US");
            fenix_lang.Expires = DateTime.Now.AddHours(24);
            Response.Cookies.Add(fenix_lang);
            Response.Redirect(Request.Url.AbsoluteUri);
            string vi_lang = "en-US";
            Thread.CurrentThread.CurrentCulture = new CultureInfo(vi_lang.ToString());
            Load_Header(Thread.CurrentThread.CurrentCulture);
            Load_Footer(Thread.CurrentThread.CurrentCulture);
        }

        private void LoadContactVN()
        {
            DataTable dt = Phoenix.AppCode.Bussiness.CorpInfoService.CorpInfoList();
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                lb_address.Text = dr["Address"].ToString();
                lb_phone.Text = dr["Phone"].ToString();
                lb_fax.Text = dr["Fax"].ToString();
                lb_mail.Text = dr["Email"].ToString();
            }
        }
        private void LoadContactUS()
        {
            DataTable dt = Phoenix.AppCode.Bussiness.CorpInfoService.CorpInfoList();
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[1];
                lb_address.Text = dr["Address"].ToString();
                lb_phone.Text = dr["Phone"].ToString();
                lb_fax.Text = dr["Fax"].ToString();
                lb_mail.Text = dr["Email"].ToString();
            }
        }
        private void LoadDB_VN()
        {
            string srt = "";
            int id = 3;
            string lang = "vi-VN";
            left_cag.ID_ArticleCategory = id;
            left_cag.Language = lang;
            DataTable dt = Phoenix.AppCode.Bussiness.ArticleService.ArticleGetById(left_cag);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string url = "http://localhost:1109/Details.aspx?Id=" + dt.Rows[i]["ID_Article"];
                srt += "<li><a href='" + url + "'>" + dt.Rows[i]["TitleName"] + "</a></li>";
            }
            left_category.Text = srt;
        }
        private void LoadDB_US()
        {
            string srt = "";
            int id = 3;
            string lang = "en-US";
            left_cag.ID_ArticleCategory = id;
            left_cag.Language = lang;
            DataTable dt = Phoenix.AppCode.Bussiness.ArticleService.ArticleGetById(left_cag);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string url = "http://localhost:1109/Details.aspx?Id=" + dt.Rows[i]["ID_Article"];
                srt += "<li><a href='" + url + "'>" + dt.Rows[i]["TitleName"] + "</a></li>";
            }
            left_category.Text = srt;
        }
        private void getCounter()
        {
            DataTable dt = Phoenix.AppCode.Bussiness.CounterService.GetCounter(online);
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                lb_online.Text = dr["Hitcounter"].ToString();
            }
        }
    }
}